package de.clapp.backend.network;

import java.io.IOException;

import com.google.gson.Gson;

import de.clapp.backend.requests.ServerRequest;

public class ClientRequest implements AutoCloseable
{
    private TCPSocket socket;

    private Gson gson;

    public ClientRequest(TCPSocket socket) throws IOException
    {
        this.socket = socket;
//        this.socket.setSoTimeout(5000);
        this.gson = new Gson();
    }
    
    public void request(ServerRequest request) throws IOException
    {
        this.socket.send(request);
        request.setItem(gson.fromJson((String) this.socket.receive(), request.getReturnType()));
    }

//    public <T> T request(ServerRequest request, Class<T> cls) throws IOException
////    public <T> T request(ServerRequest request) throws IOException
////    public <T> T request(ServerRequest request, T cls) throws IOException
//    {
//        this.socket.send(request);
////        return gson.fromJson((String) this.socket.receive(), request.getReturnType());
//
//        return (T) gson.fromJson((String) this.socket.receive(), request.getReturnType());
//
////  10.03.2013      return (T) cls.cast(gson.fromJson((String) this.socket.receive(), request.getReturnType()));
//    }

    @Override
    public void close() throws Exception
    {
        this.socket.close();
    }
}
